package com.itheima.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest2 {

    @Autowired
    private UserMapper userMapper;

    // 条件查询，普通语法   SELECT * FROM tb_user WHERE age > 25
    @Test
    public void test01() throws Exception {
        // 动态条件
        Integer age = null;
        // 查询对象
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (age != null) {
            wrapper.gt("age", age);  // age > 25
        }
        List<User> list = userMapper.selectList(wrapper);
        System.out.println(list);
    }

    // 条件查询，lambda语法 SELECT * FROM tb_user WHERE age > 25
    @Test
    public void test02() throws Exception {
        // 动态条件
        Integer age = null;
        // 查询对象
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(age!=null,User::getAge, age); // age > 25
        List<User> list = userMapper.selectList(wrapper);
    }
}